package com.wwj.app.v1;

import com.github.pagehelper.Page;
import com.wwj.core.wawa.entity.Room;
import com.wwj.core.wawa.entity.UploadFile;
import com.wwj.core.wawa.service.IUploadFileService;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.*;

@Controller
@RequestMapping("/v1/file")
public class FileUploadController extends BaseController {

    @Resource
    private IUploadFileService uploadFileService;

    @RequestMapping("/list")
    @ResponseBody
    public ReturnData list(@RequestParam int playerId, @RequestParam(required = false) Integer pageNo, @RequestParam(required = false) Integer pageSize) {
        ReturnData returnData = new ReturnData();
        try {
            if(pageNo == null || pageNo == 0) {
                pageNo = 1;
            }
            if(pageSize == null || pageSize ==0) {
                pageSize = 10;
            }
            int offset = (pageNo - 1) * pageSize;
            int limit = pageSize;
            Page<UploadFile> uploadFiles = uploadFileService.findByPlayerId(playerId, offset, limit);
            returnData.success(uploadFiles);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public ReturnData upload(@RequestParam int playerId, MultipartHttpServletRequest request) {
        ReturnData returnData = new ReturnData();
        try {
            Map<String,MultipartFile> multipartFileMap = request.getFileMap();
            Iterator<String> it = multipartFileMap.keySet().iterator();
            List<Map<String, Object>> files = new ArrayList<Map<String, Object>>();
            while(it.hasNext()){
                MultipartFile multipartFile = multipartFileMap.get(it.next());
                if (multipartFile != null && !multipartFile.isEmpty()) {
                    File localFile = saveLocal(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
                    Map<String, Object> file = new HashMap<String, Object>();
                    file.put("name", multipartFile.getOriginalFilename());
                    file.put("size", multipartFile.getSize());
                    file.put("contentType", multipartFile.getContentType());
                    file.put("file", localFile);
                    files.add(file);
                }
            }
            uploadFileService.dealUploadFile(playerId, files);
            returnData.success();
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    protected File saveLocal(InputStream inputStream, String fileName) throws Exception {
        File file = new File(getClass().getResource("").getPath(), System.currentTimeMillis() + fileName);
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            fos.write(buffer, 0, length);
            fos.flush();
        }
        fos.close();
        inputStream.close();
        return file;
    }
}
